import { Radio } from 'antd'
import React, { forwardRef, useState } from 'react'

// 定义封装后组件的属性类型
interface CustomRadioGroupProps extends Omit<React.ComponentProps<typeof Radio.Group>, 'value' | 'onChange'> {
  defaultValue?: string | number // 自定义默认值
  onChange?: (value: string | number) => void // 自定义变更处理器
}

const CustomRadioGroup = forwardRef<HTMLDivElement, CustomRadioGroupProps>(
  ({ defaultValue, onChange, ...props }, ref) => {
    const [value, setValue] = useState(defaultValue)

    const handleChange = (val: string | number) => {
      setValue(val)
      onChange && onChange(val)
    }

    return <Radio.Group ref={ref} value={value} onChange={({ target }) => handleChange(target.value)} {...props} />
  }
)

export default CustomRadioGroup
